﻿namespace ScrumLite.WebUx.Tests
{
    using System;
    using System.Collections.Generic;
    using ScrumLite.Services;
    using ScrumLite.Services.Contracts;
    using System.Linq;

    public class MockDeliverablesService : IDeliverablesService
    {
    
        public bool AddDeliverableCalled { get; set; }
        public bool UpdateDeliverableCalled { get; set; }
        public Deliverable DeliverableAdded { get; set; }
        public Deliverable DeliverableUpdated { get; set; }

        public List<string> ProjectToReturn { get; private set; }
        public Dictionary<string, List<Deliverable>> DeliverablesToReturn { get; private set;}

        public MockDeliverablesService()
        {
            MockRequestsService requestService = new MockRequestsService();
            ProjectToReturn = new List<string>(new string[] {
                        "Project1",
                        "Project2",
                        "Project3",
                        "Project4",
                        "AF06A611-12CE-4C06-B3AD-E2754D12D516"
                    });

            WorkArea[] workAreas = requestService.GetWorkAreas(ProjectToReturn[4]);            
          
            DeliverablesToReturn = new Dictionary<string, List<Deliverable>>();

            DeliverablesToReturn.Add(ProjectToReturn[0], new List<Deliverable>(new Deliverable[] {
            new Deliverable() { Name = "Deliverable 1", Moniker = 0, Url = new Uri("http://svnrepo.scrumlite.org/projectTest/deliverable1.zip"), ReleasedOn = DateTime.Now, Id = new Guid("AC8C14E8-122D-4efb-A1B3-AA5F54FED647") },
            new Deliverable() { Name = "Deliverable 2", Moniker = 1, Url = new Uri("http://svnrepo.scrumlite.org/projectTest/deliverable2.zip"), ReleasedOn = DateTime.Now, Id = new Guid("06A7A778-CA32-4e91-A4CB-3C3B53BA9C89") },
            new Deliverable() { Name = "Deliverable 3", Moniker = 2, Url = new Uri("http://svnrepo.scrumlite.org/projectTest/deliverable3.zip"), ReleasedOn = DateTime.Now, Id = new Guid("DE1C2C1A-29ED-4972-81A9-1F9C9440F68B") },
            }));

            DeliverablesToReturn.Add(ProjectToReturn[1], new List<Deliverable>(new Deliverable[] {
            new Deliverable() { Name = "Deliverable 2", Moniker = 0, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable2.zip"), ReleasedOn = DateTime.Now },
            new Deliverable() { Name = "Deliverable 3", Moniker = 1, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable3.zip"), ReleasedOn = DateTime.Now },
            new Deliverable() { Name = "Deliverable 4", Moniker = 2, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable4.zip"), ReleasedOn = DateTime.Now },
            new Deliverable() { Name = "Deliverable 5", Moniker = 3, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable5.zip"), ReleasedOn = DateTime.Now },
            }));

            
            DeliverablesToReturn.Add(ProjectToReturn[4], new List<Deliverable>(new Deliverable[] {
            new Deliverable() { Name = "Deliverable1", Moniker = 1, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable1.zip"), ReleasedOn = DateTime.Now, Id = new Guid("A06A8A24-7DAF-408A-A0B1-3D63E5D42F29"), WorkAreas = workAreas },
            new Deliverable() { Name = "Deliverable2", Moniker = 2, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable2.zip"), ReleasedOn = DateTime.Now, Id = new Guid("B06A8A24-7DAF-408A-A0B1-3D63E5D42F29") },
            new Deliverable() { Name = "Deliverable3", Moniker = 3, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable3.zip"), ReleasedOn = DateTime.Now, Id = new Guid("C06A8A24-7DAF-408A-A0B1-3D63E5D42F29"), WorkAreas = workAreas, IsDefective = true },
            new Deliverable() { Name = "Deliverable4", Moniker = 4, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable4.zip"), ReleasedOn = DateTime.Now, Id = new Guid("D06A8A24-7DAF-408A-A0B1-3D63E5D42F29") },
            new Deliverable() { Name = "Deliverable5", Moniker = 5, Url = new Uri("http://svnrepo.scrumlite.org/projectTest1/deliverable5.zip"), ReleasedOn = DateTime.Now, Id = new Guid("E06A8A24-7DAF-408A-A0B1-3D63E5D42F29"), WorkAreas = workAreas, IsDefective = true },
            }));
        }

        public Deliverable[] GetDeliverables(string projectName, string projectId)
        {
            if (String.IsNullOrEmpty(projectId))
            {
                return DeliverablesToReturn.ContainsKey(projectName) ? DeliverablesToReturn[projectName].ToArray() : null;
            }
            else
            {
                return null;
            }
        } 

        public Deliverable[] GetDeliverables(string projectId)
        {
            List<Deliverable> result = new List<Deliverable>();
            if (DeliverablesToReturn.ContainsKey(projectId))
            {
                result = DeliverablesToReturn[projectId];
            }

            return result.ToArray();
        }

        public Deliverable GetDeliverable(string id)
        {
            return DeliverablesToReturn[ProjectToReturn[4]].First(algo => algo.Id.Equals( new Guid(id)));
        }

        public Guid GetDeliverableByMoniker(string projectId, int moniker)
        {
            Guid result = Guid.Empty;
            if (DeliverablesToReturn.ContainsKey(projectId))
            {
                Deliverable deliverable = DeliverablesToReturn[projectId].First(algo => algo.Moniker == moniker);
                result = deliverable.Id;
            }
            return result;
        }

        public void Add(string projectId, Deliverable deliverable)
        {
            AddDeliverableCalled = true;
            DeliverableAdded = deliverable;
            deliverable.Id = Guid.NewGuid();            

            if (DeliverablesToReturn.ContainsKey(projectId))
            {
                List<Deliverable> list = DeliverablesToReturn[projectId];
                list.Add(deliverable);
                DeliverablesToReturn[projectId] = list;
            }
            else
            {
                DeliverablesToReturn[projectId] = new List<Deliverable>() { deliverable };
            }
        }

        public void Update(string projectId, Deliverable deliverable)
        {
            UpdateDeliverableCalled = true;
            DeliverableUpdated = deliverable;

            if (DeliverablesToReturn.ContainsKey(projectId))
            {
                List<Deliverable> list = DeliverablesToReturn[projectId];

                Deliverable originalDeliverable = (from de in list
                                                   where de.Id == deliverable.Id
                                                   select de).SingleOrDefault();
                list.Remove(originalDeliverable);
                list.Add(deliverable);
                DeliverablesToReturn[projectId] = list;
            }
            else
            {
                DeliverablesToReturn[projectId] = new List<Deliverable>() { deliverable };
            }
        }

        public Deliverable[] GetDeliverablesByWorkArea(WorkArea workArea)
        {
            throw new NotImplementedException();
        }

        public WorkArea[] GetWorkAreas(string deliverableId)
        {
            List<Deliverable> list = DeliverablesToReturn[ProjectToReturn[4]];

            Deliverable deliverable = (from de in list
                                       where de.Id.ToString() == deliverableId
                                       select de).SingleOrDefault();

            return deliverable.WorkAreas;
        }       


        public int GetNextMoniker(string projectId)
        {
            throw new NotImplementedException(projectId);
        }

    }
}
